﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Model;

namespace Portal.Admin.Screen.Render
{
    public class ProcessAjaxHandlerBll
    {
        public static string GetSelectRole(string SiteId)
        {
            try
            {
                Portal.Systems.Model.RoleCls[] Roles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRolesForUser(SiteId, WebSessionUtility.GetCurrentLoginUser().UserId);
                if (Roles.Length == 0)
                {
                    WebSessionUtility.GetCurrentLoginUser().RoleId = "";
                }
                else
                {
                    if (string.IsNullOrEmpty(WebSessionUtility.GetCurrentLoginUser().RoleId))
                    {
                        WebSessionUtility.GetCurrentLoginUser().RoleId = Roles[0].RoleId;
                        WebSessionUtility.GetCurrentLoginUser().RoleName = Roles[0].RoleName;
                    }
                }

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string SelectRoleText =
                    "<script language=javascript>\r\n" +
                    "   function ChangeRole()\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       RoleId=document.getElementById('drpSelectRoleForMenu').value;\r\n" +
                    //"       AjaxOut = Portal.Admin.Screen.Render.ProcessAjaxHandlerBll.ServerSideChangeRole(SiteId,RoleId).value;\r\n" +
                    //"       if(AjaxOut.Error)\r\n" +
                    //"       {\r\n" +
                    //"           alert(AjaxOut.InfoMessage);\r\n" +
                    //"           return;\r\n" +
                    //"       }\r\n" +
                    //"       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "       window.open('"+OSiteParam.HttpRoot+"/Admin/ChangeRole.aspx?RoleId='+RoleId,'_self');\r\n"+
                    "   }\r\n" +
                    "</script>\r\n" +
                    "<select id=\"drpSelectRoleForMenu\" onchange=\"javascript:ChangeRole();\">\r\n";
                for (int iIndex = 0; iIndex < Roles.Length; iIndex++)
                {
                    if (WebSessionUtility.GetCurrentLoginUser().RoleId.Equals(Roles[iIndex].RoleId))
                    {
                        SelectRoleText += "   <option selected value=\"" + Roles[iIndex].RoleId + "\">" + Roles[iIndex].RoleName + "</option>\r\n";
                    }
                    else
                    {
                        SelectRoleText += "   <option value=\"" + Roles[iIndex].RoleId + "\">" + Roles[iIndex].RoleName + "</option>\r\n";
                    }
                }
                SelectRoleText += "</select>\r\n";
                return SelectRoleText;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public AjaxOut ServerSideChangeRole(string SiteId, string RoleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                RoleCls ORoleCls = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateRoleModel(SiteId, RoleId);
                WebSessionUtility.GetCurrentLoginUser().RoleId = RoleId;
                WebSessionUtility.GetCurrentLoginUser().RoleName = ORoleCls.RoleName;
                OAjaxOut.RetUrl = "home.aspx";
            }
            catch (Exception ex)
            {
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
